package com.tb.shoop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tb.v1.api.shoop.ISpCategoryService;
import com.tb.v1.api.vo.SpCategoryVo;
import com.tb.v1.common.exception.CommonException;
import com.tb.v1.entity.SpCategory;
import com.tb.v1.result.ResultJson;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: tangBin
 * @Date: 2020/7/20 0020 15:45
 * @Description:
 */
@RequestMapping("/v1")
@RestController
public class SpCategoryController extends BaseController {

    @Reference
    private ISpCategoryService spCategoryService;

    /**
     * @return com.tb.v1.result.ResultJson<java.lang.Object>
     * @Author tangBin
     * @Description 商品分类列表  三级分类
     * @Date 2020/7/20 0020 15:48
     * @Param [vo]
     **/
    @RequestMapping("category")
    public ResultJson<Object> getCategoryList(SpCategoryVo vo) {
        Map map = new HashMap();
        if (!StringUtils.isEmpty(vo.getPageIndex())) {
            Integer page = (vo.getPageIndex() - 1) * vo.getPageSize();
            map.put("page", page);
            map.put("size", vo.getPageSize());
        }
        map.put("level", 0);
        //获取第一级分类
        List<SpCategory> categoryList = spCategoryService.getCategoryList(map);
        List oneList = new ArrayList();
        if (!categoryList.isEmpty()) {
            Map paramsMap = new HashMap();
            List<SpCategory> categoryList1 = null;
            List<SpCategory> categoryList2 = null;
            Map oneMap = null;
            Map twpMap = null;
            List twoList = null;
            for (SpCategory spCategory : categoryList) {
                twoList = new ArrayList();
                //获取一级下面二级分类
                paramsMap.put("pid", spCategory.getCatId());
                paramsMap.put("level", 1);
                categoryList1 = spCategoryService.getCategoryList(paramsMap);
                for (SpCategory spCategory1 : categoryList1) {
                    //获取二级下面三级分类
                    paramsMap.put("pid", spCategory1.getCatId());
                    paramsMap.put("level", 2);
                    categoryList2 = spCategoryService.getCategoryList(paramsMap);

                    twpMap = JSON.parseObject(JSON.toJSONString(spCategory1));
                    twpMap.put("children", categoryList2);
                    twoList.add(twpMap);
                }
                oneMap = JSON.parseObject(JSON.toJSONString(spCategory));
                oneMap.put("children", twoList);
                oneList.add(oneMap);
            }
        }
        Integer totalNum = 0;
        if (!StringUtils.isEmpty(vo.getPageIndex())) {
            totalNum = spCategoryService.getCategoryListCount(map);
        }
        Map resultMap = new HashMap();
        resultMap.put("total", totalNum);
        resultMap.put("pageIndex", vo.getPageIndex());
        resultMap.put("resultList", oneList);
        return toResultModel("200", "成功", resultMap);
    }

    /**
     * @return com.tb.v1.result.ResultJson<java.lang.Object>
     * @Author tangBin
     * @Description 商品分类 获取前二级分类列表
     * @Date 2020/7/20 0020 18:23
     * @Param [vo]
     **/
    @RequestMapping("category/two")
    public ResultJson<Object> getCategoryTwoList(SpCategoryVo vo) {
        Map map = new HashMap();
        map.put("level", 0);
        //获取第一级分类
        List<SpCategory> categoryList = spCategoryService.getCategoryList(map);
        List oneList = new ArrayList();
        if (!categoryList.isEmpty()) {
            Map paramsMap = new HashMap();
            List<SpCategory> categoryList1 = null;
            Map oneMap = null;
            for (SpCategory spCategory : categoryList) {
                //获取一级下面二级分类
                paramsMap.put("pid", spCategory.getCatId());
                paramsMap.put("level", 1);
                categoryList1 = spCategoryService.getCategoryList(paramsMap);
                oneMap = JSON.parseObject(JSON.toJSONString(spCategory));
                oneMap.put("children", categoryList1);
                oneList.add(oneMap);
            }
        }
        Integer totalNum = spCategoryService.getCategoryListCount(map);
        Map resultMap = new HashMap();
        resultMap.put("total", totalNum);
        resultMap.put("pageIndex", vo.getPageIndex());
        resultMap.put("resultList", oneList);
        return toResultModel("200", "成功", resultMap);
    }

    /**
     * @return com.tb.v1.result.ResultJson<java.lang.Object>
     * @Author tangBin
     * @Description 添加分类
     * @Date 2020/7/21 0021 10:11
     * @Param [vo]
     **/
    @RequestMapping("add/category")
    public ResultJson<Object> getAddCategory(@RequestBody SpCategoryVo vo) throws Exception {
        if (StringUtils.isEmpty(vo.getCatName()) ||
                StringUtils.isEmpty(vo.getCatPid()) ||
                StringUtils.isEmpty(vo.getCatLevel())) {
            throw new CommonException("500", "参数为空");
        }
        SpCategory category = new SpCategory();
        category.setCatLevel(vo.getCatLevel());
        category.setCatName(vo.getCatName());
        category.setCatPid(vo.getCatPid());
        Integer num = spCategoryService.saveEntity(category);
        if (num > 0) {
            return toResultModel("200", "成功");
        } else {
            return toResultModel("200", "成功");
        }
    }

    /**
     * @return com.tb.v1.result.ResultJson<java.lang.Object>
     * @Author tangBin
     * @Description 修改分类
     * @Date 2020/7/21 0021 10:11
     * @Param [vo]
     **/
    @RequestMapping("edit/category/{catId}")
    public ResultJson<Object> getUpdateCategory(@PathVariable("catId") String catId,
                                                String catName) throws Exception {
        if (StringUtils.isEmpty(catId) ||
                StringUtils.isEmpty(catName)) {
            throw new CommonException("500", "参数为空");
        }
        SpCategory category = new SpCategory();
        category.setCatName(catName);
        category.setCatId(Integer.valueOf(catId));
        Integer num = spCategoryService.updateEntity(category);
        if (num > 0) {
            return toResultModel("200", "成功");
        } else {
            return toResultModel("200", "成功");
        }
    }

    /**
     * @return com.tb.v1.result.ResultJson<java.lang.Object>
     * @Author tangBin
     * @Description 修改分类
     * @Date 2020/7/21 0021 10:11
     * @Param [vo]
     **/
    @RequestMapping("delete/category/{catId}")
    public ResultJson<Object> getDeleteCategory(@PathVariable("catId") String catId) throws Exception {
        if (StringUtils.isEmpty(catId)) {
            throw new CommonException("500", "参数为空");
        }
        Integer num = spCategoryService.deleteEntity(Long.valueOf(catId));
        if (num > 0) {
            return toResultModel("200", "成功");
        } else {
            return toResultModel("200", "成功");
        }
    }
}
